$(function () {
    $(".del_p_btn").click(function () {
        p_id = $(this).attr('p_id');
        params = {
            "p_id": p_id
        };
        $.ajax({
            url: "/del_product.html",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == 0) {
                    $(".dropdown-item[p_id=" + resp.p_id + "]").remove();
                    count = $("#count_my_cart").text();
                    $("#count_my_cart").text(parseInt(count) - 1);
                    var last_num = parseInt($("#total_price").text().slice(1));
                    $("#total_price").text("$" + (last_num - parseInt(resp.del_price)))
                } else {
                    alert("删除失败")
                }

            }
        })

    });

    // 结账按钮点击
    $(".checkout_btn").click(function () {
        var products = {};
        $(".buy:checked").each(function () {
            products[$(this).attr("p_id")] = $(this).parent().parent().next().next().next().next().find("input").val();
        });
        params = {
            "products": products
        };
        $.ajax({
            url: "/get_data.html",
            type: "post",
            data: JSON.stringify(params),
            contentType: "application/json",
            success: function (resp) {
                if (resp.errno == 0) {
                    // alert('结账');
                    window.location.href = '/checkout.html'
                } else {
                    alert("失败")
                }
            }

        })
    })
});